package com.zhdl.modules.puncturecable.service.impl;

import com.project.common.util.DelayUtil;
import com.zhdl.common.ytools.HexToBitTools;
import com.zhdl.hardware.plc.siemens.service.SiemensCmdSendService;
import com.zhdl.hardware.robot.fair.service.FairCmdSendService;
import com.zhdl.modules.puncturecable.service.PunctureCableService;
import com.zhdl.modules.spacer.ProcessTest;
import com.zhdl.modules.spacer.entity.SpacerParameters;
import com.zhdl.modules.spacer.service.SpacerParametersService;
import com.zhdl.modules.tools.CommonCallback;
import com.zhdl.modules.tools.IntToHexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Service
public class PunctureCableServiceImpl implements PunctureCableService {
    /**
     * 上料点位
     * 原点	/f/bIII63III201III151IIIMoveJ(34.921,-53.871,111.209,-147.138,90.000,102.079,-405.5,-407.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 原点上方(抓料姿势)	/f/bIII63III201III146IIIMoveJ(34.921,-76.852,98.522,-111.670,90.000,102.079,-405.5,-407.499,520,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 原点上方(放料姿势)	/f/bIII63III201III148IIIMoveJ(34.921,-80.271,73.128,-82.858,-90.000,106.921,-405.5,-407.499,520,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 抓料点	/f/bIII63III201III150IIIMoveJ(48.696,-52.593,108.286,-145.525,90.000,88.304,-307.5,-504.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 抓料安全点	/f/bIII63III201III150IIIMoveJ(48.445,-49.148,101.025,-141.763,90.000,88.239,-337.5,-534.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 放料点上方	/f/bIII63III201III165IIIMoveJ(14.647,-75.574,66.698,-81.645,-90.001,86.649,-553.793,-250.168,525.476,-179.969,0.52,17.999,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 放料点	/f/bIII63III201III153IIIMoveJ(14.648,-75.575,78.228,-92.653,-90.000,86.648,-557.072,-251.034,445.319,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     */
    String[] pick_actions = new String[]{
            "/f/bIII63III201III151IIIMoveJ(34.921,-53.871,111.209,-147.138,90.000,102.079,-405.5,-407.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III146IIIMoveJ(34.921,-76.852,98.522,-111.670,90.000,102.079,-405.5,-407.499,520,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III148IIIMoveJ(34.921,-80.271,73.128,-82.858,-90.000,106.921,-405.5,-407.499,520,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III150IIIMoveJ(48.696,-52.593,108.286,-145.525,90.000,88.304,-307.5,-504.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III150IIIMoveJ(48.445,-49.148,101.025,-141.763,90.000,88.239,-337.5,-534.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III165IIIMoveJ(14.647,-75.574,66.698,-81.645,-90.001,86.649,-553.793,-250.168,525.476,-179.969,0.52,17.999,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III153IIIMoveJ(14.648,-75.575,78.228,-92.653,-90.000,86.648,-557.072,-251.034,445.319,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f"
    };

    /**
     * 待机位 /f/bIII63III201III151IIIMoveJ(47.19,-44.487,-122.344,-103.062,89.686,-126.611,200,65,500,179.684,0.102,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 等待抓取螺杆 /f/bIII63III201III151IIIMoveJ(77.352,-51.437,-120.442,-98.187,89.675,-96.448,150,200,440,179.684,0.102,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 抓取螺杆 /f/bIII63III201III151IIIMoveJ(79.998,-59.736,-117.207,-93.138,86.678,-93.802,150,260,440,179.684,0.102,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 螺杆上方 /f/bIII63III201III152IIIMoveJ(79.998,-60.826,-107.924,-101.330,89.678,-93.802,150,260,500,179.684,0.102,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 涂胶位 /f/bIII63III201III152IIIMoveJ(37.189,-89.590,-62.855,-177.394,89.709,-136.811,420,190,630,179.684,0.102,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 涂胶上抬 /f/bIII63III201III152IIIMoveJ(37.189,-89.590,-62.855,-117.395,89.709,-136.611,420,190,660,179.684,0.102,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 托盘/f/bIII63III201III154IIIMoveJ(-3.65,-98.879,-74.129,-96.988,89.668,-108.65,525.84,-111.84,500,179.684,0.102,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * /f/bIII63III201III156IIIMoveJ(-1.007,-94.248,-82.241,-93.521,89.668,-106.007,525.84,-111.84,500,179.684,0.102,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * /f/bIII63III201III154IIIMoveJ(1.944,-90.028,-86.627,-93.373,89.669,-103.057,499.68,-85.68,500,179.684,0.102,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * /f/bIII63III201III153IIIMoveJ(5.247,-85.941,-90.544,-93.552,89.671,-99.753,473.52,-59.52,500,179.684,0.102,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * /f/bIII63III201III152IIIMoveJ(8.952,-81.99,-94.052,-94.026,89.675,-96.048,447.36,-33.36,500,179.684,0.102,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     */
    String[] put_action = new String[]{
            "/f/bIII63III201III144IIIMoveJ(47.018,-44.570,-122.298,-103.062,90.000,-126.782,200,65,440,180,0,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III143IIIMoveJ(77.209,-51.456,-120.456,-98.091,90.000,-96.591,150,200,440,180,0,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III143IIIMoveJ(79.884,-59.744,-117.224,-93.032,90.000,-93.916,150,260,440,180,0,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III143IIIMoveJ(79.884,-66.800,-84.983,-118.317,90.000,-93.916,150,260,630,180,0,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III144IIIMoveJ(37.125,-87.778,-69.383,-112.839,90.000,-136.675,420,190,630,180,0,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III144IIIMoveJ(37.125,-89.663,-62.736,-117.601,90.000,-136.675,420,190,660,180,0,83.8,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III144IIIMoveJ(-3.709,-105.947,-43.741,-120.311,90.000,-108.709,552,-138,660,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III142IIIMoveJ(-3.709,-98.583,-78.114,-93.303,90.000,-108.709,552,-138,495,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III150IIIMoveJ(-1.070,-100.829,-50.326,-118.846,90.000,-106.070,525.84,-111.84,660,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III148IIIMoveJ(-1.070,-94.224,-82.997,-92.779,90.000,-106.070,525.84,-111.84,495,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III147IIIMoveJ(1.877,-96.195,-55.798,-118.007,90.000,-103.123,499.68,-85.68,660,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III146IIIMoveJ(1.877,-90.002,-87.386,-92.612,90.000,-103.123,499.68,-85.68,495,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III146IIIMoveJ(5.176,-91.927,-60.434,-117.639,90.000,-99.824,473.52,-59.52,660,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III145IIIMoveJ(5.176,-85.909,-91.322,-92.769,90.000,-99.824,473.52,-59.52,495,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III146IIIMoveJ(8.878,-87.969,-64.387,-117.643,90.000,-96.122,447.36,-33.36,660,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f",
            "/f/bIII63III201III145IIIMoveJ(8.878,-81.949,-94.831,-93.220,90.000,-96.122,447.36,-33.36,495,180,0,15,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f"
    };

    @Override
    public List<String[]> setGetLocation(){
        List<String[]> ls = new ArrayList<>();
        ls.add(pick_actions);
        ls.add(put_action);
        return ls;
    }

    String[] put_action_name = new String[]{
            "待机位",
            "等待螺杆",
            "抓取螺杆",
            "螺杆上方",
            "涂胶位",
            "涂胶上方",
            "放料位上1",
            "放料位1",
            "放料位上2",
            "放料位2",
            "放料位上3",
            "放料位3",
            "放料位上4",
            "放料位4",
            "放料位上5",
            "放料位5"
    };

    Map<String, String> put_actions = new ConcurrentHashMap<>();

    @Override
    public void init() {
        for (int i = 0; i < 16; i++) {
            put_actions.put(put_action_name[i], put_action[i]);
        }
    }

    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;


    /**
     * 机器人_上料动作
     *
     * @param actionId 0 原点
     * @param callback 回调
     */
    @Override
    public void robotSendPickAction(String port, int actionId, CommonCallback<String> callback) {
        robotSendPickAction(port, pick_actions[actionId], s -> {
            log.info("机器人-指令发送成功" + s);
        });
        callback.statusCallback("ok");
    }

    @Override
    public void robotSendPickAction(String port, String actionCmd, CommonCallback<String> callback) {
        fairCmdSendService.FairActionRun(port, actionCmd, s -> {
            log.info("机器人-指令发送成功" + s);
        });
        callback.statusCallback("ok");
    }

    @Override
    public void robotSendPutAction(String port, String actionName, CommonCallback<String> callback) {
        robotSendPutActionCmd(port, put_actions.get(actionName), s -> {
            log.info("机器人-指令发送成功" + s);
        });
        callback.statusCallback("ok");
    }

    @Override
    public void robotSendPutActionCmd(String port, String actionCmd, CommonCallback<String> callback) {
        fairCmdSendService.FairActionRun(port, actionCmd, s -> {
            log.info("机器人-指令发送成功" + s);
        });
        callback.statusCallback("ok");
    }

    String getLocation = "/f/bIII52III377III18IIIGetActualTCPPose()III/b/f";
    String siemensValue = "";
    String siemensWarn = "";
//    String plcPort = "192.168.0.1:502";
    String plcPort = "192.168.0.101:502";
    String plcPort1 = "192.168.0.1:503";
    /**
     * 线程管理
     */
    Map<String, ScheduledFuture<?>> mapSF = new HashMap<>();
    @Resource
    private FairCmdSendService fairCmdSendService;
    @Resource
    private SiemensCmdSendService siemensCmdSendService;


    @Override
    public void robotReadLocation(String port, CommonCallback<String> callback) {
        taskScheduler1.scheduleWithFixedDelay(() ->
                fairCmdSendService.FairGetJointPos(port, getLocation, s -> {
                    log.info("机器人-世界坐标读取完成" + s);
                    /*
                        处理机器人坐标
                        String asciiLocation = AsciiToHexTools.hexToAscii(s);
                        String[] split = asciiLocation.split("III");
                        callback.statusCallback(split[4]);
                     */
                    callback.statusCallback(s);
                }), 200);
    }

    /**
     * 将byte转换为一个长度为8的byte数组，数组每个值代表bit
     */
    public static Byte[] getBitArray(byte b) {
        Byte[] array = new Byte[8];
        for (int i = 7; i >= 0; i--) {
            array[i] = (byte) (b & 1);
            b = (byte) (b >> 1);
        }
        return array;
    }

    /**
     * PLC-获取电机寄存器地址
     * 轴一 write 0-34 read 34-48
     *
     * @param model 电机型号
     * @return 寄存器地址
     */
    public String motorModelAddress(int model) {
        int addDec = (model - 1) * 24;
        return IntToHexUtil.shortToHex(addDec);
    }

    /**
     * 寄存器-地址变大操作
     *
     * @param hexAddress 十六进制寄存器地址
     * @param upNum      要增大的数字
     * @return 十六进制地址
     */
    public String hexAddressUp(String hexAddress, int upNum) {
        int addressDec = Integer.parseInt(hexAddress, 16);
        upNum = upNum / 2;
        addressDec = addressDec + upNum;
        return IntToHexUtil.shortToHex(addressDec);
    }


    /**
     * @param address  寄存器地址
     * @param bit      比特位数
     * @param value    寄存器值
     * @param callback 回调
     */
    @Override
    public void plcWriteValue(int address, int bit, int value, CommonCallback<String> callback) {
        if (!siemensValue.isEmpty()) {
            String hexAddress = IntToHexUtil.shortToHex(address);
            String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, hexAddress);
            String afterHexValue = HexToBitTools.writeValueByBit(hexValue, bit, value);
            siemensCmdSendService.siemensWriteValue(plcPort, hexAddress, afterHexValue);
            callback.statusCallback("ok");
        } else {
            plcWriteValue(address, bit, value, callback);
        }
    }

    public String writeValueByTwoBit(String hex, int bit, int value) {
        String s = HexToBitTools.writeValueByBit(hex, bit + 1, value);
        if (value == 0) {
            return HexToBitTools.writeValueByBit(s, bit, 1);
        } else if (value == 1) {
            return HexToBitTools.writeValueByBit(s, bit, 0);
        }
        return null;
    }

    /**
     * 机器人-plc信号通知
     *
     * @param model    控制哪一个
     * @param open     打开
     * @param callback 回调
     */
    @Override
    public void robotToPlc(int model, boolean open, CommonCallback<String> callback) {

        if (siemensValue.isEmpty()){
            return;
        }
        /*
         * 写入值
         */
        int value;
        if (open) {
            value = 1;
        } else {
            value = 0;
        }
        /*
            写入位数
         */
        int bit = -1;
        switch (model) {
            case 0 -> {
                /* 上料机 上料 */
                bit = 3;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0070");
                String afterHexValue = HexToBitTools.writeValueByBit(hexValue, bit, value);
                siemensCmdSendService.siemensWriteValue(plcPort, "0070", afterHexValue);
            }
            case 1 -> {
                /* 下料导轨 允许转动 */
                bit = 9;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0070");
                String afterHexValue = HexToBitTools.writeValueByBit(hexValue, bit, value);
                siemensCmdSendService.siemensWriteValue(plcPort, "0070", afterHexValue);
            }
        }
    }

    /**
     * 机器人-环形电机控制
     *
     * @param model    控制哪一个
     * @param open     打开
     * @param callback 回调
     */
    @Override
    public void robotToMotor(int model, boolean open, CommonCallback<String> callback) {
        /*
         * 写入值
         */
        int value;
        if (open) {
            value = 1;
        } else {
            value = 0;
        }
        /*
            写入位数
         */
        int bit = -1;
        switch (model) {
            case 0 -> {
                /* 上料停止 */
                bit = 0;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = HexToBitTools.writeValueByBit(hexValue, bit, value);
                siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
            }
            case 1 -> {
                /* 上料夹爪 */
                bit = 1;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = writeValueByTwoBit(hexValue, bit, value);
                if (!afterHexValue.isEmpty()) {
                    siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
                }
            }
            case 2 -> {
                /* 环形上料气缸 */
                bit = 5;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = writeValueByTwoBit(hexValue, bit, value);
                if (!afterHexValue.isEmpty()) {
                    siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
                }
            }
            case 3 -> {
                /* 允许上料通知 */
                bit = 9;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = HexToBitTools.writeValueByBit(hexValue, bit, value);
                siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
            }
            case 4 -> {
                /* 下料夹爪 */
                bit = 3;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = writeValueByTwoBit(hexValue, bit, value);
                if (!afterHexValue.isEmpty()) {
                    siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
                }
            }
            case 5 -> {
                /* 环形下料气缸 */
                bit = 7;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = writeValueByTwoBit(hexValue, bit, value);
                if (!afterHexValue.isEmpty()) {
                    siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
                }
            }
            case 6 -> {
                /* 允许下料通知 */
                bit = 10;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = HexToBitTools.writeValueByBit(hexValue, bit, value);
                siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
            }
            case 7 -> {
                bit = 13;
                String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
                String afterHexValue = HexToBitTools.writeValueByBit(hexValue, bit, value);
                siemensCmdSendService.siemensWriteValue(plcPort, "0072", afterHexValue);
            }
        }
        if (bit != -1) {
            callback.statusCallback("ok");
        }
    }

    /**
     * 252 254
     * @param model
     * @param open
     */
    @Override
    public void robotToPlc(int model,boolean open){
        String address = "";
        if (model == 0){
            address = "007E";
        } else {
            address = "007F";
        }
        siemensCmdSendService.siemensWriteValue(plcPort, address, open ? "FFFF" : "0000");
    }

    /**
     * 224.1 224.22
     *
     * @param open
     */
    @Override
    public void robotFinishInit(boolean open) {
        String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0070");
        if (open) {
            String upHex = HexToBitTools.writeValueByBit(hexValue, 1, 1);
            siemensCmdSendService.siemensWriteValue(plcPort, "0070", upHex);
        } else {
            String downHex = HexToBitTools.writeValueByBit(hexValue, 1, 1);
            siemensCmdSendService.siemensWriteValue(plcPort, "0070", downHex);
        }
    }

    /**
     * 穿垫片-读取plc某一信号
     */
    @Override
    public void readPlc0072(CommonCallback<String> callback) {
        if (!siemensValue.isEmpty() && siemensValue.length() >= 488) {
            String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
            String bitValueByHex = HexToBitTools.readBitValueByHex(hexValue);
            callback.statusCallback(bitValueByHex);
        } else {
            log.info("读取0072的寄存器，但是读取plc获取的值不对,长度为--->" + siemensValue.length());
        }
    }

    /**
     * 225.3
     * @param callback 回调
     */
    @Override
    public void readStopStatus(CommonCallback<String> callback) {
        if (!siemensValue.isEmpty() && siemensValue.length() >= 488) {
            String hexValue = HexToBitTools.readHexValueByAddress(siemensValue, "0070");
            String bitValueByHex = HexToBitTools.readBitValueByHex(hexValue);
            callback.statusCallback(bitValueByHex);
        } else {
            log.info("读取0070的寄存器，但是读取plc获取的值不对,长度为--->" + siemensValue.length());
        }
    }

    /**
     * 穿垫片-读取plc信号  -126
     * 358
     */
    @Override
    public void readPlcSignal(CommonCallback<String> callback) {
        siemensCmdSendService.siemensAddressValue(plcPort, "0000", 122, 300, s -> {
            siemensValue = s;
            log.info("读取到的结果为" + s);
            callback.statusCallback(s);
        });
    }

    /**
     * 240-241 242-243 244-245 246-247 248-249 250-251
     * @param callback
     */
    @Override
    public void readPlcWarnSignal(CommonCallback<String> callback) {
        siemensCmdSendService.siemensAddressValue(plcPort, "0078", 6, 300, s -> {
            siemensWarn = s;
            log.info("读取到的结果为" + s);
            callback.statusCallback(s);
        });
    }

    /**
     *  十六进制转为位报警信息
     *  报警信息  已经反转过了,每一个字节 前后反转过了
     *  西门子的plc  高低位不用变，只反转了字节的位
     */
    public String warnList(String siemensWarn){
        return HexToBitTools.readBitValueByHex(siemensWarn);
    }

    /**
     * 清料停止过程中  自动运行已停止 报警复位
     * 225.2  清料停止过程中  1清料 0停止
     * 225.3  0 运行，1停止
     *
     */
    public String statusList(String siemensValue){
        String hexValueByAddress = HexToBitTools.readHexValueByAddress(siemensValue, "0070");
        String bitValueByHex = HexToBitTools.readBitValueByHex(hexValueByAddress);
        /* 清料停止 */
        String bitByBits1 = HexToBitTools.getBitByBits(bitValueByHex, 10);
        String bitByBits2 = HexToBitTools.getBitByBits(bitValueByHex, 11);
        return bitByBits1 + bitByBits2;
    }

    /**
     * 报警复位
     */
    public void writeWarnReset(String siemensValue){
        String hexValueByAddress = HexToBitTools.readHexValueByAddress(siemensValue, "0072");
        String bitValueByHex = HexToBitTools.readBitValueByHex(hexValueByAddress);
        String writeValueByBit = HexToBitTools.writeValueByBit(hexValueByAddress, 12, 1);
        log.info("之前的值为" + bitValueByHex + ",修改后的值为" + writeValueByBit);
    }


    String luoganLength = "";

    @Resource
    SpacerParametersService spacerParametersService;


    public String readPeiFang(){
        String hexValueByAddress1 = HexToBitTools.readHexValueByAddress(siemensValue, "0003");
        String hexValueByAddress11 = HexToBitTools.readHexValueByAddress(siemensValue, "0004");
        String hexValueByAddress2 = HexToBitTools.readHexValueByAddress(siemensValue, "0005");
        String hexValueByAddress22 = HexToBitTools.readHexValueByAddress(siemensValue, "0006");
        String typeHex = hexValueByAddress1 + hexValueByAddress11;
        String modelRunHex = hexValueByAddress2 + hexValueByAddress22;

        int i1 = Integer.parseInt(hexValueByAddress11);
        int i2 = Integer.parseInt(hexValueByAddress22);

        if (i1 == 1){
            siemensCmdSendService.siemensAddressValue1(plcPort, "0092", 1, s -> {
                luoganLength = s;
            });
        } else {
            siemensCmdSendService.siemensAddressValue1(plcPort, "0096", 1, s -> {
                luoganLength = s;
            });
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        int i3 = Integer.parseInt(luoganLength, 16);


        SpacerParameters spacerParameters = new SpacerParameters();
        spacerParameters.setModelId(i1);
        spacerParameters.setRodLength(i3);
        spacerParameters.setType(i2);
        spacerParametersService.update(spacerParameters,1);

        return i1 + "-" + i2 + "-" + i3;
    }



    @Override
    public void test(CommonCallback<String> callback){
        siemensCmdSendService.test(s -> {
            log.info("读取到的结果为" + s);
            callback.statusCallback("ok");
        });
    }

    @Override
    public void readPlcSignal1(CommonCallback<String> callback) {
        siemensCmdSendService.siemensAddressValue(plcPort1, "0000", 126, 300, s -> {
            siemensValue = s;
            log.info("读取到的结果为" + s);
            callback.statusCallback(s);
        });
    }

    /**
     * 读取plc到位信号
     *
     * @param model 指令
     */
    @Override
    public String readPlcReachSignal(int model){
        String hexValue1 = HexToBitTools.readHexValueByAddress(siemensValue, "0074");
        String bitValue1 = HexToBitTools.readBitValueByHex(hexValue1);

        String hexValue2 = HexToBitTools.readHexValueByAddress(siemensValue, "0075");
        String bitValue2 = HexToBitTools.readBitValueByHex(hexValue2);

        switch (model){
            case 1 -> {
                /* 上料 夹爪 开到位 */
                return bitValue1.substring(3, 4);
            }
            case 2 -> {
                /* 上料 夹爪 关到位 */
                return bitValue1.substring(4, 5);
            }
            case 3 -> {
                /* 上料 气缸 开到位 */
                return bitValue1.substring(5, 6);
            }
            case 4 -> {
                /* 上料 气缸 关到位 */
                return bitValue1.substring(6, 7);
            }
            case 5 -> {
                /* 下料 夹爪 开到位 */
                return bitValue2.substring(12, 13);
            }
            case 6 -> {
                /* 下料 夹爪 关到位 */
                return bitValue2.substring(13, 14);
            }
            case 7 -> {
                /* 下料 气缸 开到位 */
                return bitValue2.substring(10, 11);
            }
            case 8 -> {
                /* 下料 气缸 关到位 */
                return bitValue2.substring(11, 12);
            }
            default -> {
                return null;
            }
        }
    }

    /**
     * 上料点位
     * 原点	/f/bIII63III201III151IIIMoveJ(34.921,-53.871,111.209,-147.138,90.000,102.079,-405.5,-407.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 原点上方(抓料姿势)	/f/bIII63III201III146IIIMoveJ(34.921,-76.852,98.522,-111.670,90.000,102.079,-405.5,-407.499,520,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 原点上方(放料姿势)	/f/bIII63III201III148IIIMoveJ(34.921,-80.271,73.128,-82.858,-90.000,106.921,-405.5,-407.499,520,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 抓料点	/f/bIII63III201III150IIIMoveJ(48.696,-52.593,108.286,-145.525,90.000,88.304,-307.5,-504.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 抓料安全点	/f/bIII63III201III150IIIMoveJ(48.445,-49.148,101.025,-141.763,90.000,88.239,-337.5,-534.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 放料点上方	/f/bIII63III201III149IIIMoveJ(14.648,-75.575,66.697,-81.647,-90.000,86.648,-557.072,-251.034,520,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     * 放料点	/f/bIII63III201III153IIIMoveJ(14.648,-75.575,78.228,-92.653,-90.000,86.648,-557.072,-251.034,445.319,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f
     */
    String put_Origin = "/f/bIII63III201III151IIIMoveJ(34.921,-53.871,111.209,-147.138,90.000,102.079,-405.5,-407.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
    String put_OriginUpGo = "/f/bIII63III201III146IIIMoveJ(34.921,-76.852,98.522,-111.670,90.000,102.079,-405.5,-407.499,520,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
    String put_OriginUpBack = "/f/bIII63III201III148IIIMoveJ(34.921,-80.271,73.128,-82.858,-90.000,106.921,-405.5,-407.499,520,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
    String put_PickPoint = "/f/bIII63III201III150IIIMoveJ(48.696,-52.593,108.286,-145.525,90.000,88.304,-307.5,-504.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
    String put_PickSafe = "/f/bIII63III201III150IIIMoveJ(48.445,-49.148,101.025,-141.763,90.000,88.239,-337.5,-534.499,262.728,0,0,47,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
    String put_DownPointUp = "/f/bIII63III201III149IIIMoveJ(14.648,-75.575,66.697,-81.647,-90.000,86.648,-557.072,-251.034,520,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
    String put_DownPoint = "/f/bIII63III201III153IIIMoveJ(14.648,-75.575,78.228,-92.653,-90.000,86.648,-557.072,-251.034,445.319,180,0,18,0,0,100,180,50,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";

}

